package cl.duocap.refaxion.view.bean;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

//import javax.faces.context.ExternalContext;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;

import cl.duocap.refaxion.dto.UsuarioDTO;
import cl.duocap.refaxion.servicios.UsuarioService;

//import org.apache.log4j.Logger;

@ManagedBean(name = "authenticationBean")
@SessionScoped
public class AuthenticationBean  implements Serializable  {

	//final Logger log = Logger.getLogger(AuthenticationBean.class);
	
	//@Autowired
	//@Qualifier("authenticationManager")
	//protected AuthenticationManager authenticationManager;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//@Size(min = 1, message = "Username cannot be empty")
	private String usuario;
	
	@ManagedProperty("#{logeadoBean}")
    private LogeadoBean logeadoBean; 
	//@Size(min = 1, message = "Password cannot be empty")
	private String password;
	
	private UsuarioDTO usuarioLogeado = new UsuarioDTO();

	public AuthenticationBean() {

	}

	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}

	public String getUsuario() {
		return usuario;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public String login() throws IOException, ServletException {

		try {

			UsuarioService  usuarioAutenticar = new UsuarioService();
			usuarioLogeado =  usuarioAutenticar.autenticarUsuario(usuario, password);
			if (usuarioLogeado.getRut() != null){				
				logeadoBean.setUsuarioValidado(usuarioLogeado);
				if(logeadoBean.getUsuarioValidado().getIdTipoUsuario() == 2){
					return "/genericas/bandejaCliente.xhtml?faces-redirect=true";	
				}else{
					return "/genericas/bandejaUsuario.xhtml?faces-redirect=true";	
				}
			}else{
				return "/genericas/accessDenied.xhtml?faces-redirect=true";				
			}

		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage()));

			System.out.println("error1");
			//log.error(e.getMessage());
			System.out.println("error2");
			return null;
		}
	}
	
	public String logout() throws IOException {
		this.usuario = "";
		this.password = "";
		usuarioLogeado = null;
		ExternalContext context = FacesContext.getCurrentInstance()
				.getExternalContext();
		context.redirect(context.getRequestContextPath()
				+ "/genericas/logout.xhtml");
		FacesContext.getCurrentInstance().responseComplete();
		return null;
		
	}

	public LogeadoBean getLogeadoBean() {
		return logeadoBean;
	}

	public void setLogeadoBean(LogeadoBean logeadoBean) {
		this.logeadoBean = logeadoBean;
	}
	
	

	
}